package com.lvmama.config.factory;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Created by troy on 2016/7/19.
 * kouhongyu@163.com
 */
public class ZooKeeperClientFactory {

    private static CuratorFramework client;

    private static final int MAX_RETRIES = 3;
    private static final int BASE_SLEEP_TIMEMS = 3000;

    private ZooKeeperClientFactory() {

    }

    public static CuratorFramework getInstance() {
        return client;
    }

    public static CuratorFramework getInstance(final String zookeeperAddress, final String nameSpace) {
        if (client == null) {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_TIMEMS, MAX_RETRIES);
            client = CuratorFrameworkFactory.builder()
                    .connectString(zookeeperAddress)
                    .retryPolicy(retryPolicy)
                    .namespace(nameSpace)
                    .build();
            client.start();
        }
        return client;
    }
}
